/*
 * Copyright (c) 2006-2021, Ulandlink Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-09-18     lfz         first version
 */
#include <rtthread.h>
#include "app_system_sm.h"
#include "app_system_msg.h"
#include "app_system_srv.h"

#define LOG_TAG                    "system.srv"
#define LOG_LVL                    LOG_LVL_INFO
#include "ulog.h"

static void app_system_timer_fun(void *parameter)
{
    app_system_timout_msg_post();
}

static void app_system_entry(void *parameter)
{
    os_mq_data msg = {0};
    
    if ( !app_system_msg_init() ){
        LOG_E("app system msg init fail.");
        return;
    }
    
    if ( !app_system_sm_init(app_system_timer_fun) ){
        LOG_E("app system msg init fail.");
        return;
    }

    while(1) {
		memset(&msg, 0, sizeof(os_mq_data));
		if (app_system_msg_recv(&msg, RT_WAITING_FOREVER)) {		
			app_system_msg_proc(&msg);
		}
		os_mq_free_mem(&msg);
    }
}
    
int32_t app_system_init(void)
{	
    #define SYSTEM_STACK_SIZE	(2048)
    #define SYSTEM_PRIORITY     (8)
    
	rt_thread_t tid = RT_NULL;

	tid = rt_thread_create("system", 
                            app_system_entry, 
                            RT_NULL,
							SYSTEM_STACK_SIZE, 
                            SYSTEM_PRIORITY, 10);
	if (tid != RT_NULL){
        rt_thread_startup(tid);
		return RT_EOK;
	}
    LOG_E("app system init fail.");
	return -RT_ERROR;
}

INIT_APP_EXPORT(app_system_init);

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
